import { _decorator, Component, instantiate, Label, Node, NodeEventType, PageView, Slider, Sprite, Toggle, tween, Vec2, Vec3 } from 'cc';
import { UIManager } from '../UIManager';
import { LanauageManager } from '../LanauageManager';
import { GameData, linkCfg } from '../GameData';
import { HttpClient } from '../net/HttpClient';
import { EventManager } from '../EventManager';
import { AudioMgr } from '../AudioMgr';
import { Util } from '../Util';
const { ccclass, property } = _decorator;

@ccclass('activityLink')
export class activityLink extends Component {

    @property(PageView)
    shopPageView:PageView = null;

    @property(Label)
    tipLab:Label = null;

    protected start(): void {
        EventManager.eventTarget.on(EventManager.REFRESH_LINKCFGS, this.refresh, this);
        this.refresh();
    }

    refresh(){
        if (GameData.linkCfgs == null || GameData.linkCfgs.length == 0){
            return;
        }

        for (let index = 0; index < GameData.linkCfgs.length; index++) {
            let itemData:linkCfg = GameData.linkCfgs[index];
            let item = this.shopPageView.content.children[index];
            if (!item){
                item = instantiate(this.shopPageView.content.children[0])
                this.shopPageView.addPage(item);
            }
            item.active = true;

            Util.loadRemoteUrl(itemData.img, item.getComponent(Sprite));

            item.off(NodeEventType.TOUCH_END);
            item.on(NodeEventType.TOUCH_END, ()=>{
                if (itemData.url != ""){
                    window.open(itemData.url, '_blank');
                }
            },this)
        }
        this.unschedule(this.changePageView);
        if (GameData.linkCfgs.length > 2){
            this.schedule(this.changePageView, 3);
        }
    }

    changePageView(){
        let index = this.shopPageView.getCurrentPageIndex();
        if (index >= GameData.linkCfgs.length){
            index = 0;
        }else{
            index++
        }
        this.shopPageView.scrollToPage(index);
    }
}

